<!doctype html>
<html>

	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="css/mui.css" rel="stylesheet" />
		<link href="css/head.css" rel="stylesheet" />
		<link href="css/myface.css" rel="stylesheet" />
	</head>

	<body>
		<header class="mui-bar mui-bar-nav title">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left face-cion"></a>
			<a id="openMenu" class="mui-icon mui-icon-right-nav mui-pull-right" style="color: white;">…</a>
			<h1 class="mui-title title-color">个人头像</h1>
		</header>
		
	<div class="mui-content">
		<img id = "img_myface" src="image/headimg.jpg" />
	</div>
		<div id="sheet_myface" class="mui-popover mui-popover-bottom mui-popover-action ">
			<!-- 可选择菜单 -->
			<ul class="mui-table-view">
			  <li class="mui-table-view-cell">
				<a id="link_choosePhoto" href="#">选择照片</a>
			  </li>
			  <li class="mui-table-view-cell">
				<a id="link_savePhoto" href="#">下载照片</a>
			  </li>
			</ul>
			<!-- 取消菜单 -->
			<ul class="mui-table-view">
			  <li class="mui-table-view-cell">
				<a href="#sheet_myface"><b>取消</b></a>
			  </li>
			</ul>
			 
		</div>
	
		
	
		<script src="js/mui.js"></script>
		<script src="js/app.js"></script>
		<script type="text/javascript">
			mui.init()
			mui.plusReady(function(){

			refreshFace();
			// 获取手机端可见屏幕的宽度
			var imgWidth = document.body.clientWidth;
			img_myface.width = imgWidth;
			img_myface.height = imgWidth;
			
			// 添加自定义事件，刷新头像
				window.addEventListener("refresh", function(){
					refreshFace();
				 });
				
			//绑定选择菜单
			var openMenu = document.getElementById("openMenu");
			openMenu.addEventListener("tap",function(){
				mui("#sheet_myface").popover("toggle")
				
			})
				// 绑定选择照片的菜单按钮
				var link_choosePhoto = document.getElementById("link_choosePhoto");
				link_choosePhoto.addEventListener("tap", function() {
					
					mui.openWindow(
					{
						// 打开新页面
						url: "plugin/v3.1.6/myface-uploader.html",
						id:"myface-uploader.html",
						createNew: true});
					// "plugin/v3.1.6/myface-uploader.html","myface-uploader.html")
					mui("#sheet-myface").popover("toggle");
				});
				 
				// 绑定保存照片的菜单按钮 
					var link_savePhoto = document.getElementById("link_savePhoto");
				    link_savePhoto.addEventListener("tap", function() {
					
					plus.nativeUI.showWaiting("下载中")
					var user = app.getUserGlobalInfo();
					var faceImage = user.faceImageBig;
					alert(faceImage)
					var dtast = plus.downloader.createDownload(
					app.imgServerUrl+faceImage,
					{},
					function(downloadeFile,status){
						plus.nativeUI.closeWaiting();
						if(status == 200){
							var tempFile = downloadeFile.filename;
							alert(status)
							// 通过相册api保存照片到本地相册
							plus.gallery.save(tempFile, function() {
								alert("success")
								app.showToast("保存照片成功！", "success");
							})
							
						}else{
							app.showToast("下载错误...","error");
							console.log("下载错误")
						}
						
					}
					);
					//启动下载任务
					dtast.start()
				
				});
				
			
				});
				function refreshFace() {
					//刷新显示头像
					var user = app.getUserGlobalInfo();
					// alert(user.get)
						var img_myface = document.getElementById("img_myface");
				        // alert(img_myface.src)
						if(user != null) {
						var faceImage = user.faceImageBig;
						if(app.isNotNull(faceImage)) {
						img_myface.src = app.imgServerUrl + faceImage;
						// alert(img_myface.src)
					   }
					}
				}
			
		</script>
	</body>

</html>
